<?php

class WapController extends BackendController
{
    public $layout='/layouts/column2';
    public $city_en;
    public function actionInfo(){

        //所属城市
        $city_id = Yii::app()->admin->getState('city');
        $this->city_en = Yii::app()->admin->getState('city_en');
        //获取WEIXIN配置
        $info = CityApp::model()->find('city_id=:city_id and type=:type',array(':city_id'=>$city_id,':type'=>'weixin'));
        $this->render('info',array('info'=>$info,'city_en'=> $this->city_en));
    }
    public function actionGetCode(){
        $ut = CHtml::encode(Yii::app()->request->getParam('ut'));
        $url = Yii::app()->createAbsoluteUrl('index/index');
        $code = XUtils::qrcode($url,false);
        echo $code;
    }
    public function actionGetCode1(){
        $str = '1|http://www.1wedo.com';
        $secret = Xtutuquan::encryption($str)    ;
        $code = XUtils::qrcode($secret,false);
        echo $code;
    }
    public function actionAddApp(){
        //所属城市
        $city_id = Yii::app()->admin->getState('city');
        $this->city_en = Yii::app()->admin->getState('city_en');
        //获取WEIXIN配置
        $info = CityApp::model()->find('city_id=:city_id and type=:type',array(':city_id'=>$city_id,':type'=>'weixin'));
        if($info){
            if($info->status==0){
                $data['status'] = 0;
                $data['message'] = '总后台已暂停使用此功能';
                echo json_encode($data);
                Yii::app()->end();
            }
            //修改参数
            $info->wx_guid = CHtml::encode($_POST['wx_guid']);
            $info->wx_name = CHtml::encode($_POST['wx_name']);
            $info->wx_id = CHtml::encode($_POST['wx_id']);
            $info->wx_app_id =  CHtml::encode($_POST['wx_app_id']);
            $info->wx_app_secret =  CHtml::encode($_POST['wx_app_secret']);
            $info->company_phone =  CHtml::encode($_POST['company_phone']);
            $info->is_type = $_POST['is_type'];
            if($info->update()){
                $data['status'] = 1;
                $data['message'] = '修改成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '修改失败';
            }
        }else{
            //新增
            $db = new CityApp();
            $db->city_id = $city_id;
            $db->en_name = $this->city_en;
            $db->status = 1;
            $db->user_token = Xtutuquan::randStr();
            $db->wx_guid = CHtml::encode($_POST['wx_guid']);
            $db->wx_name = CHtml::encode($_POST['wx_name']);
            $db->wx_id = CHtml::encode($_POST['wx_id']);
            $db->wx_token = Xtutuquan::randStr();
            $db->wx_api_url = Yii::app()->createAbsoluteUrl('app/api').'?ut='.$db->user_token;
            $db->wx_app_id =  CHtml::encode($_POST['wx_app_id']);
            $db->wx_app_secret =  CHtml::encode($_POST['wx_app_secret']);
            $db->company_phone =  CHtml::encode($_POST['company_phone']);
            $db->is_type = $_POST['is_type'];
            $db->type =  'weixin';
            if($db->save()){
                $data['status'] = 1;
                $data['message'] = '生成成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '生成失败';
            }
        }
        echo json_encode($data);
        Yii::app()->end();
    }
}